package cn.com.surker.material.controller;

import cn.com.surker.common.AbstractController;
import cn.com.surker.dto.main.MainSupplierQueryDTO;
import cn.com.surker.dto.main.MainSupplierUpdateDTO;
import cn.com.surker.material.service.MainSupplierService;
import cn.com.surker.material.service.MaterialSupplierService;
import cn.com.surker.oa.entity.OaSupplier;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.vo.main.MainSupplierVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/mainsupplier")
public class MainSupplierController extends AbstractController {


    @Resource
    private MainSupplierService mainSupplierService;
    @Resource
    private MaterialSupplierService materialSupplierService;

    /**
     * 获取供应商详情方法
     * @param id
     * @return
     */
    @GetMapping(value = "/detail")
    public RestResultDto queryDetail(@RequestParam("id") Long id){
        MainSupplierVO mainSupplierVO = mainSupplierService.queryDetail(id);
        return RestResultDto.success(mainSupplierVO);
    }

    /**
     * 供应商分页查询方法
     * @param dto
     * @return
     */
    @PostMapping(value = "/page")
    public RestResultDto pageData(@RequestBody MainSupplierQueryDTO dto){
        return RestResultDto.success(mainSupplierService.pageData(dto));
    }

    /**
     * 供应商更新方法
     * @param dto
     * @return
     */
    @PostMapping(value = "/update")
    public RestResultDto update(@RequestBody MainSupplierUpdateDTO dto){
        CacheUser user = getUser();
        return RestResultDto.success(mainSupplierService.updateMainSpupplier(dto,user));
    }

    /**
     * 供应商更新方法
     * @param dto
     * @return
     */
    @PostMapping(value = "/updatestatus")
    public RestResultDto updatestatus(@RequestBody MainSupplierUpdateDTO dto){
        CacheUser user = getUser();
        return RestResultDto.success(mainSupplierService.updatestatus(dto,user));
    }

    /**
     * 根据公司标识获取供应商数据
     * @param dto
     * @return
     */
    @PostMapping(value = "/getList")
    public RestResultDto listByOrgId(@RequestBody MainSupplierQueryDTO dto){
        return RestResultDto.success(mainSupplierService.listByOrgId(dto));
    }
    /**
     * 根据公司查询供应商信息
     * @return
     */
    @PostMapping(value = "/by-org")
    public RestResultDto byOrg(Long orgId){
       try {
           List<OaSupplier> list = materialSupplierService.queryByOrgV1(orgId);
           if (list == null || list.size() == 0)
               return RestResultDto.fail("未获取到供应商信息");
           return RestResultDto.success(list);
       } catch (Exception e) {
           e.printStackTrace();
           return RestResultDto.fail("获取供应商信息异常");
       }
    }

}
